package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserBuilding;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.*;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class UserBuildingDaoImpl extends TempDataObjectDao<UserBuilding> {

	public Map<Integer, UserBuilding> getUserBuildingMap(long userId) {
		Map<Integer, UserBuilding> dataMap = new ConcurrentHashMap<>();
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_u_building where userId = ? order by buildingId desc";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setLong(1, userId);
			rs = call.executeQuery();
			while (rs.next()) {
				UserBuilding data = parse(rs);
				dataMap.put(data.getBuildingId(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	@Override
	protected boolean addData(UserBuilding data) {
		PreparedStatement call = null;
		String sql = "INSERT INTO t_u_building(userId,buildingId,staffCount,lastUpdateTime,level,patronsIdList) VALUES(?,?,?,?,?,?)";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setLong(i++, data.getUserId());
			call.setInt(i++, data.getBuildingId());
			call.setInt(i++, data.getStaffCount());
			call.setLong(i++, data.getLastUpdateTime());
			call.setInt(i++, data.getLevel());
			call.setString(i++, StringUtils.listToString(data.getPatronsIdList(), ','));
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected boolean updateData(UserBuilding data) {
		PreparedStatement call = null;
		String sql = "UPDATE t_u_building SET staffCount=?,lastUpdateTime=?,level=?,patronsIdList=? where userId=? and buildingId=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setInt(i++, data.getStaffCount());
			call.setLong(i++, data.getLastUpdateTime());
			call.setInt(i++, data.getLevel());
			call.setString(i++, StringUtils.listToString(data.getPatronsIdList(), ','));
			call.setLong(i++, data.getUserId());
			call.setInt(i++, data.getBuildingId());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected UserBuilding parseData(ResultSet rs) throws SQLException {
		UserBuilding data = new UserBuilding();
		data.setBuildingId(rs.getInt("buildingId"));
		data.setUserId(rs.getLong("userId"));
		data.setStaffCount(rs.getInt("staffCount"));
		data.setLastUpdateTime(rs.getLong("lastUpdateTime"));
		data.setLevel(rs.getInt("level"));
		List<Integer> idList = StringUtils.stringToIntegerListNoZero(rs.getString("patronsIdList"), ",");
		List<Integer> patronsIdList = new ArrayList<>();
		for(Integer id : idList){
			if(!patronsIdList.contains(id)){
				patronsIdList.add(id);
			}
		}
		data.setPatronsIdList(patronsIdList);
		return data;
	}
}